
var updateBookModel;
(function(){
    var table= layui.table;
    var form =layui.form;
    table.render({
        elem: '#demo'
        ,url: '../Book/findByPage' //数据接口
        ,height: 500
        ,cols: [[ //表头
            {type: 'checkbox', fixed: 'left'}
            ,{field: 'putbookId', title: 'ID', width:70, sort: true, fixed: 'left'}
            ,{field: 'putbookName', title: '书名', width:100}
            ,{field: 'suppName', title: '供货商', width:240}
            ,{field: 'putbookNum', title: '库存', width:100}
            ,{field: 'areaName', title: '区域', width: 80}

        ]]
        ,limit: 10
        ,page: true
        ,response: {
            statusCode: 200 //重新规定成功的状态码为 200，table 组件默认为 0
        }
        ,parseData: function(res) { //res 即为原始返回的数据
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "count": res.data.total, //解析数据长度
                "data": res.data.list //解析数据列表
            };
        },where:{
            "suppName":layui.$("input[name='searchSuppName']").val(),
            "putbookName":layui.$("input[name='searchPutbookName']").val()

        }
    });

    form.on('submit(updateBookBtn)', function (data) {

        var putbookId = layui.$("#editBookForm input[name='putbookId']").val();
        var putbookName = data.field.putbookName;
        var suppName = data.field.suppName;
        var putbookNum = data.field.putbookNum;
        var areaName = data.field.areaName;
        layui.$.ajax({
            url: "../Book/updateBook",
            method: "put",
            data: {
                "putbookId":putbookId,
                "putbookName":putbookName,
                "suppName":suppName,
                "putbookNum":putbookNum,
                "areaName":areaName
            },
            success: function (data) {
                if (data.code == 200) {
                    layui.layer.close(updateBookModel);
                    layui.layer.msg(data.msg, {icon: 1});
                    setTimeout(function () {
                    },1000)
                    layui.table.reload("demo");

                } else if (data.code == 500) {
                    layui.layer.close(updateBookModel);
                    layui.layer.alert(data.msg, {icon: 2});
                    setTimeout(function () {
                    },1000)
                }
            },
            error: function (data) {
            }
        });
        return false;
    });
})();
function editBook() {
//获取被选中项
    var checkStatus = layui.table.checkStatus('demo');
    if (checkStatus.data.length ==0){
        layui.layer.msg("请选择数据",{icon:2})
        setTimeout(function () {
        },1000)
        return;
    }
    if(checkStatus.data.length >1){
        layui.layer.msg("请选择一条数据",{icon:2});
        setTimeout(function () {
        },1000)
        return;
    }
    var putbookId = checkStatus.data[0].putbookId;
    //从后台加载数据
    layui.$.ajax({
        url:"../Book/Book/"+putbookId,
        method:"get",
        success:function (result) {
            //填充到编辑框中
            layui.$("#editBookModel input[name='putbookId']").val(result.data.putbookId);
            layui.$("#editBookModel input[name='putbookName']").val(result.data.putbookName);
            layui.$("#editBookModel input[name='suppName']").val(result.data.suppName);
            layui.$("#editBookModel input[name='putbookNum']").val(result.data.putbookNum);
            layui.$("#editBookModel input[name='areaName']").val(result.data.areaName);
            //展示编辑框
            updateBookModel = layui.layer.open({
                type: 1,
                title: "编辑图书信息",
                content: layui.$("#editBookModel")
            });
            //点击确定提交

        }
    });
}
function  removeBook() {
    //获取被选中项
    //通过Ajax 发起异步删除请求
    var checkStatus = layui.table.checkStatus('demo'); //idTest 即为基础参数 id 对应的值
    if(checkStatus.data.length==0){
        layui.layer.msg("请选择数据",{icon:2});
        setTimeout(function () {
        },1000)
        return;
    }
    var putbookIds=new Array();
    for (let i=0 ;i<checkStatus.data.length; i++ ){
        putbookIds.push(checkStatus.data[i].putbookId);
    }
    layui.$.ajax({
        url:"../Book/batchDelete?putbookIds="+putbookIds.join(),
        method: "delete",
        success:function (data) {
            layui.layer.msg(data.msg,{icon:1});
            setTimeout(function () {
            },1000)
            layui.table.reload("demo");
        },error: function(data){
        }
    })

}
function searchOutBook(){
    layui.table.reload("demo",{
        where:{
            "suppName":layui.$("input[name='searchSuppName']").val(),
            "putbookName":layui.$("input[name='searchPutbookName']").val()
        }
    });
}
function exportExcel() {
    var putbookName = layui.$("input[name='searchPutbookName']").val();
    var a= document.createElement("a");
    a.href="../Book/exportExcel?putbookName="+putbookName;
    a.style.display="none";
    document.body.appendChild(a);
    a.click();
}
